module.exports = function (i) {
  let http = require('http')
  let configs = require('./configs')
  let cheerio = require('cheerio')
  let peek = require('./peek')
  let choose = require('./choose')
  let search = require('./search')
  let getViewstate = require('./getViewState')
// let server=new http.Server();
  let studentId = configs.studentId
// let studentId = "160225"//学号
  let exp_name = configs.whichExp[i] //选哪个实验
  let week = configs.week
  // let weekday = configs.weekday
  // let period = configs.period
  let mode = configs.mode
  let concurrent = configs.concurrent// 每个 http 请求并发数 不要超过20, 连接总数立方增长
//61 光的干涉实验（一）薄膜干涉（牛顿环）dx

//62 迈克尔逊干涉仪的调节和使用

//63 分光计的调节和使用

//64 光栅衍射

//65 光的干涉实验（二）双棱镜干涉

//66 交流电路的谐振现象

//67 设计用伏安法测量电阻（采用补偿测量）

//68 用霍尔元件测量磁场

//69 气垫导轨上滑块的运动

//70 用超声光栅测定液体中的声速

//71 太阳能电池的特性测量

//72 声速的测量

//73 密立根油滴测电子电荷

//74 全息照相


  console.log('searching')
  search(studentId, exp_name, week, function (html) {//相当于点击搜索，按实验名称搜索
    let $ = cheerio.load(html)
    let courses_to_choose_tbody = $('#ID_PEE110101_gvpee110201')
    let courses_choosed_tbody = $('#ID_PEE110101_gvpee110202')

    let courses_to_choose_rows = courses_to_choose_tbody.find('tr')//获取数据表
    let courses_choosed_rows = courses_choosed_tbody.find('tr')//获取数据表
    // 获取省下合适时间段选课按钮id
    let btnId = [];
    let t_btnId= []
    for (let i = 1; i < courses_choosed_rows.length; i++) {
      let row = $(courses_choosed_rows[i])
      if (row.text().indexOf('退')>=0) {
        let id = row.find('a').attr('id').replace(/_/g, '$')
        let tmp = id.split('')
        tmp.splice(2, 1, '_')
        t_btnId.push(tmp.join(''))

      }
    }
    for (let i = 1; i < courses_to_choose_rows.length; i++) {
      let row = $(courses_to_choose_rows[i])
      if (!(row.text().indexOf("星期二") >= 0 && row.text().indexOf("第二时段") >= 0)
        &&!(row.text().indexOf("第一时段")>=0)) {
        let id;
        if(row.find('a').attr('id')===undefined) {
          return
        } else {
          id = row.find('a').attr('id').replace(/_/g, '$')
        }

        let tmp = id.split('')
        tmp.splice(2, 1, '_')
        btnId.push(tmp.join(''))
      }
    }

    // console.log(btnId)
    // console.log(t_btnId)
    let viewstate = getViewstate(html)
    // console.log(viewstate)
    if (mode === 'choose') {
      for (let j = 0; j < concurrent; j++) {// 每个按钮点多少次
        console.log('choosing')
        let r = Math.floor(Math.random() * btnId.length)
        choose(studentId, viewstate, btnId[r], function (html) { // 随机选
          // console.log(html)
          if (html.indexOf('成功') >= 0) {
            for(let k=0;k<50;k++) {
              console.log('Success')
            }
          } else if(html.indexOf('该实验课名额已满，请选修其他节次实验课') >= 0){
            for(let k=0;k<50;k++) {
              console.log('该实验课名额已满，请选修其他节次实验课')
            }
          } else if (html.indexOf('该课程您所选的实验已经选完') >= 0){
            for(let k=0;k<50;k++) {
              console.log('该课程您所选的实验已经选完')
            }
          } else if (html.indexOf('本周实验已经选完') >= 0) {
            for(let k=0;k<50;k++) {
              console.log('本周实验已经选完')
            }
          } else {
            console.log('未知错误')
          }
        })
      }
    } else if(mode === 'discard') {
      // for (let j = 0; j < concurrent; j++) {// 每个按钮点多少次
        console.log('discarding')
        choose(studentId, viewstate, t_btnId[t_btnId.length - 1], function (html) {// 退最后一个
          if (html.indexOf('成功') >= 0) {
            console.log('DISCARDED!!!')
          }
        })
      // }
    }
    else {
      console.log('<table>' + courses_to_choose_tbody.html() + '</table>')
      console.log('<table>' + courses_choosed_tbody.html() + '</table>')
    }
  })



}